package com.rp.controller;

import com.rp.pojo.User;
import com.rp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/findPage")
    public List<User> findPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "10") Integer pageSize) {
        return userService.findPage(page, pageSize);
    }

    @GetMapping(value = "/downLoadXlsByJxl", name = "使用jxl下载EXCEL")
    public void downLoadXlsByJxl(HttpServletResponse response) {
        userService.downLoadXlsByJxl(response);
    }


    @PostMapping(value = "/uploadExcel", name = "上传用户数据")
    public void uploadExcel(MultipartFile file) throws Exception {
        userService.uploadExcel(file);
    }

    @PostMapping(value = "/uploadExcelByEasyPoi", name = "使用EasyPOI上传用户数据")
    public void uploadExcelByEasyPoi(MultipartFile file) throws Exception {
        userService.uploadExcelByEasyPOI(file);
    }


    @GetMapping(value = "/downLoadXlsxByPoi", name = "使用poi下载[高版本]")
    public void downLoadXlsxByPoi(HttpServletResponse response) throws Exception {
        //导出Excel
        userService.downLoadXlsxByPoi(response);
    }

    @GetMapping(value = "/downLoadXlsxByPoiWithStyle", name = "使用poi下载[高版本](原生带样式)EXCEL")
    public void downLoadXlsxByPoiWithStyle(HttpServletResponse response) throws Exception {
        //导出带样式的Excel
        userService.downLoadXlsxByPoiWithStyle(response);
    }

    @GetMapping(value = "/downLoadXlsxByPoiWithTemplate", name = "使用poi下载[高版本](使用模板)EXCEL")
    public void downLoadXlsxByPoiWithTemplate(HttpServletResponse response) throws Exception {
        //根据模板导出带样式的Excel
        userService.downLoadXlsxByPoiWithTemplate(response);
    }


    @GetMapping(value = "/download", name = "使用poi下载用户详细信息")
    public void download(Long id, HttpServletResponse response) throws Exception {
        //使用模板下载用户详情信息
        userService.downloadUserInfoByTemplate(id, response);
    }

    @GetMapping(value = "/download1", name = "使用poi模板下载用户详细信息")
    public void download1(Long id, HttpServletResponse response) throws Exception {
        //使用模板引擎下载用户详情信息
        userService.downloadUserInfoByTemplate2(id, response);
    }

    @GetMapping(value = "/download2", name = "使用easy-poi + 模板下载用户详细信息")
    public void download2(Long id, HttpServletResponse response) throws Exception {
        //使用easypoi和模板下载用户信息
        userService.downloadUserInfoByTemplate3(id, response);
    }

    @GetMapping(value = "/downLoadMillion", name = "使用sax 下载百万数据Excel(百万数据的导出)")
    public void downLoadMillion(HttpServletResponse response) throws Exception {
        userService.downLoadMillion(response);
    }


    @GetMapping(value = "/downLoadCSV", name = "会用poi导出用户数据到CSV文件中")
    public void downLoadCSV(HttpServletResponse response) throws Exception {
        userService.downLoadCSV(response);
    }


    @GetMapping(value = "/downLoadCSVByEasyPIO", name = "导出用户数据到CSV文件中(通过easy-poi)")
    public void downLoadCSVByEasyPIO(HttpServletResponse response) throws Exception {
        userService.downLoadCSVByEasyPIO(response);
    }

    @GetMapping(value = "/{id}", name = "查询用户详情信息")
    public User findUserInfo(@PathVariable("id") Long id) throws Exception {
        User user = userService.findUserInfo(id);
        return user;
    }


    @GetMapping(value = "/downloadContract", name = "通过模板下载word合同")
    public void downloadContract(@RequestParam Long id, HttpServletResponse response) throws Exception {
        userService.downloadContract(id, response);
    }


    @GetMapping(value = "/downLoadWithEasyPOI", name = "使用poi导出用户数据")
    public void downLoadWithEasyPOI(HttpServletResponse response) throws Exception {
        userService.downLoadWithEasyPOI(response);
    }

    /**
     * 注意: 不支持 表单中含有图片的导出
     * @param id
     * @param response
     * @throws Exception
     */
    @GetMapping(value = "/downloadContractByEasyPOI", name = "通过EasyPOI模板下载word合同")
    public void downloadContractByEasyPOI(@RequestParam Long id, HttpServletResponse response) throws Exception {
        userService.downloadContractByEasyPOI(id, response);
    }
}
